package com.kgc.springboot.aviation.controller.jianghongkun;

import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.pojo.Consulting;
import com.kgc.springboot.aviation.service.jianghongkun.ZixunService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * ClassName: ConsultingController <br/>
 * Description: <br/>
 * date: 2021/1/11 14:52<br/>
 *
 * @author 12161<br />
 * @since JDK 1.8
 */
@Controller
public class ConsultingController {
    @Resource
    ZixunService zixunService;

    @RequestMapping("/zixunliebiao")
    public String zixun(@RequestParam(value = "phone",required = false,defaultValue = "") String phone,
                        @RequestParam(value = "content",required = false,defaultValue = "") String content,
                        @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                        Model model){
        System.out.println("----------进入到咨询列表控制器--------------");
        System.out.println("从前端获取的手机号为"+phone);
        System.out.println("从前端获取的咨询内容为"+content);
        model.addAttribute("phone",phone);
        model.addAttribute("content",content);
        //设置页容量
        Integer pageSize = 10;
        PageInfo<Consulting> consultingList = zixunService.getConsultingList(phone, content, pageNum, pageSize);
        System.out.println("aaaaaaaaaa");

        if (consultingList != null){
            System.out.println("根据条件查询有数据");
            model.addAttribute("pageInfo",consultingList);
            return "zixunlist";
        }else {
            System.out.println("根据条件查询没有数据");
            return "redirect:/tiaozhuan22";
//            return "index2";
        }
    }
    @ResponseBody
    @RequestMapping("/tiaozhuan22")
    public void tiaozhuan(HttpServletResponse response, HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.write("<script>alert('查询没有数据');location.href='/zixunliebiao'</script>");
    }
}
